﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Drawing;
using System.IO;

namespace MyDesktopMicroBlog
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        System.Windows.Forms.NotifyIcon notifyIcon = new System.Windows.Forms.NotifyIcon();
        private void Application_Startup(object sender, StartupEventArgs e)
        {
            this.initNotifyIcon();
        }
        private void initNotifyIcon()
        {
           Stream stream = Application.GetResourceStream(new Uri("/icon.ico", UriKind.Relative)).Stream;
            notifyIcon.Icon = new System.Drawing.Icon(stream);
            stream.Close();
            notifyIcon.Text = "我的微博";
            notifyIcon.Visible = true;
            notifyIcon.Click += new EventHandler(notifyIcon_Click);

            System.Windows.Forms.ContextMenu cmMenu = new System.Windows.Forms.ContextMenu();
            System.Windows.Forms.MenuItem miClose = new System.Windows.Forms.MenuItem();
            miClose.Text = "退出";
            miClose.Click += new EventHandler((a, b) => { this.Shutdown(0); });

            System.Windows.Forms.MenuItem miAuthorWebsite = new System.Windows.Forms.MenuItem();
            miAuthorWebsite.Text = "作者微博";
            miAuthorWebsite.Click += new EventHandler((o, e) =>
            {
                System.Diagnostics.Process.Start(Application.Current.Resources["AuthorWebSite"].ToString());
            });

            cmMenu.MenuItems.Add(0,miAuthorWebsite);
            cmMenu.MenuItems.Add(1,miClose);
            notifyIcon.ContextMenu = cmMenu;
        }

        
        private void Application_Exit(object sender, ExitEventArgs e)
        {
            //remove notifyIcon
            this.notifyIcon.Dispose();
        }
        void notifyIcon_Click(object sender, EventArgs e)
        {
            this.MainWindow.Show();
            if (this.MainWindow.Top <= 0)
            {
                this.MainWindow.Top = 0;
            }
            if (this.MainWindow.WindowState == System.Windows.WindowState.Minimized)
            {
                this.MainWindow.WindowState = System.Windows.WindowState.Normal;
            }
            this.MainWindow.Activate();
        }
    }
}
